//
//  RHMqttDevRegionListController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/11.
//

#import "RHMqttDevRegionListController.h"
#import "RHDevRegionModel.h"
#import "EnvironmentManage.h"

@interface RHMqttDevRegionListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) __block NSInteger selectIndex;

@property (nonatomic ,strong) RHDevRegionModel *regionModel;


@end

@implementation RHMqttDevRegionListController


- (instancetype)initWithModel:(RHDevRegionModel *)model{
    self = [super init];
    if(self){
        _regionModel = model;
    }
    return self;
}

- (NSArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSArray array];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _selectIndex = -1;
    [self.navigationItem setTitle:@"服务列表"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
    
    [self getDataFromNetwork];
}

- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 0, 0, 0));
    }];
}


- (void)getDataFromNetwork{
    
    kWeakSelf;
    [QMUITips showLoadingInView:self.view];
    [RHDevRegionModel startWithBaseUrl:[EnvironmentManage sharedEnvirManage].getBaseAPIUrl APIKey:G_getCountries argument:@{} requestMethod:RequestMethodPOST requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.data){
            weakSelf.dataArray = resultModel.data;
            [weakSelf.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                RHDevRegionModel *model = obj;
                if([model.countryCode isEqualToString:self.regionModel.countryCode]&&[model.countryName isEqualToString:self.regionModel.countryName]){
                    weakSelf.selectIndex = idx;
                }
            }];
        }
        [weakSelf.tableView reloadData];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    RHDevRegionModel *model = self.dataArray[indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@(%@)",model.countryName,model.countryCode]];
    [cell.textLabel setTextColor:indexPath.row == self.selectIndex?CFMainColor:kCOLOR_HEX(@"666666")];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _selectIndex = indexPath.row;
    RHDevRegionModel *model = self.dataArray[indexPath.row];
    if(self.callBackModel){
        
        self.callBackModel(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
    [tableView reloadData];
}

@end
